<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class OrdersStatusTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function changeStatus($oderIds,$value)
	{
		$data = array(
			'status'       =>$value,
		);
		$updateStatus=$this->tableGateway->update($data,array('(ads_order_id IN ('.$oderIds.'))'));
		return 	$updateStatus;			
	}
	public function getOrderinfo($orderId){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_ads_main', 'clf_ads_main.ads_order_id=clf_ads_order.ads_order_id',array('*'),'left');
		$select->join('clf_users', 'clf_ads_order.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_user_details', 'clf_ads_order.user_id=clf_user_details.user_id',array('*'),'left');
		$select->where('clf_ads_order.ads_order_id="'.$orderId.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;	
	}
	public function getonlineorderslistsGrid($searchkey,$aColumns,$data,$userId,$distId,$areaId,$locId,$catId,$subcatId,$adMode,$paginated=false)
	{
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users', 'clf_ads_order.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_user_details', 'clf_users.user_id=clf_user_details.user_id',array('*'),'left');	
		$select->join('clf_ads_main', 'clf_ads_order.ads_order_id=clf_ads_main.ads_order_id',array('*'),'left');
		if($userId!='' && $catId=='' && $subcatId==''){
			$select->where('clf_ads_main.district_id="'.$distId.'"');
			$select->where('clf_ads_main.area_id="'.$areaId.'"');
			$select->where('clf_ads_main.location_id="'.$locId.'"');
		}else if($userId!='' && $catId!='' && $subcatId==''){
			$select->where('clf_ads_main.district_id="'.$distId.'"');
			$select->where('clf_ads_main.area_id="'.$areaId.'"');
			$select->where('clf_ads_main.location_id="'.$locId.'"');
			$select->where('clf_ads_main.category_id="'.$catId.'"');
		}else if($userId!='' && $catId!='' && $subcatId!=''){
			$select->where('clf_ads_main.district_id="'.$distId.'"');
			$select->where('clf_ads_main.area_id="'.$areaId.'"');
			$select->where('clf_ads_main.location_id="'.$locId.'"');
			$select->where('clf_ads_main.category_id="'.$catId.'"');
			$select->where('clf_ads_main.sub_cat_id="'.$subcatId.'"');
		}else if($userId=='' && $catId!='' && $subcatId==''){
			$select->where('clf_ads_main.category_id="'.$catId.'"');			
		}else if($userId=='' && $catId!='' && $subcatId!=''){
			$select->where('clf_ads_main.category_id="'.$catId.'"');
			$select->where('clf_ads_main.sub_cat_id="'.$subcatId.'"');			
		}		
		$select->where('clf_ads_order.ads_mode="'.$adMode.'"');
		// Search
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('clf_users.user_name', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}
		// End		
		// Sort Order
		/* if(isset($data['iSortCol_0']))
		{
			$sOrder="";
			for($i=0;$i<intval($data['iSortingCols']);$i++)
			{
				if($data['bSortable_'.intval($data['iSortCol_'.$i])] == "true")
				{
					$sOrder.=$aColumns[intval($data['iSortCol_'.$i])]." ".$data['sSortDir_'.$i].",";
				}
			}
			$sOrder = substr_replace($sOrder,"", -1);
		}
		if($sOrder!=''){
			$select->order($sOrder);
		} */
		// End		
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			$select->order('clf_ads_order.ads_order_id DESC')->limit($currentPage)->offset($start);
		}
		// End	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getcount($data='')
	{
		$select = $this->tableGateway->getSql()->select();
		if(isset($data['sSearch'])){
			if($data['sSearch']!='0' && $data['sSearch']!='')
			{
				$select->where(array(
					new Predicate\PredicateSet(
						array(
						new Predicate\Like('ads_mode', '%'.$data['sSearch'].'%'),
						),
							Predicate\PredicateSet::COMBINED_BY_OR
					),
				));
			}
		}		
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->count();
	}	
	public function getOverAllOrderstotal($userId){
		$select = $this->tableGateway->getSql()->select();	
		$select->where('user_id="'.$userId.'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function addOrderstatus()
    {
		if(isset($_SESSION['admin']['user_type_id']) && $_SESSION['admin']['user_type_id']=='1'){
			$userId=$_SESSION['admin']['user_id'];
			$userTypeid=$_SESSION['admin']['user_type_id'];
			$status=1;			
		}else if(isset($_SESSION['user']['user_type_id']) && $_SESSION['user']['user_type_id']=='2'){
			$userId=$_SESSION['user']['user_id'];
			$userTypeid=$_SESSION['user']['user_type_id'];
			$status=0;			
		}else if(isset($_SESSION['user']['user_type_id']) && $_SESSION['user']['user_type_id']=='3'){
			$userId=$_SESSION['user']['user_id'];
			$userTypeid=$_SESSION['user']['user_type_id'];
			$status=0;			
		}
		$data = array(
			'ads_mode' 	           => $_SESSION['orders']['ad_mode'],  		
			'tot_amount' 	       => $_SESSION['orders']['total_amount'], 	
			'discount_amount' 	   => $_SESSION['orders']['discount_amount'], 	
			'discount_percentage'  => $_SESSION['orders']['discount_percent'],  	
			'order_status' 	       => 'pending',   
			'payment_status'	   => 'pending', 	
			'added_date'  	       => date('Y-m-d H:i:s'),
			'updated_date' 	       => date('Y-m-d H:i:s'),
			'amount_paid' 	       => '',			
			'user_id' 	           => $userId, 
			'tx_id' 	           => '', 		
			'order_by' 	           => $userTypeid, 		
			'status' 	           => $status, 					
		);	
		$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;	
	}
	public function viewOrderList($userId){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_ads_main', 'clf_ads_order.ads_order_id=clf_ads_main.ads_order_id',array('*'),'left');
		$select->join('clf_users', 'clf_ads_order.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_user_details', 'clf_ads_order.user_id=clf_user_details.user_id',array('*'),'left');
		$select->where('clf_ads_order.user_id="'.$userId.'"');
		$select->order('clf_ads_order.ads_order_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function viewOrderListWithCategory($userId,$ordertype,$cid,$scid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_ads_main', 'clf_ads_order.ads_order_id=clf_ads_main.ads_order_id',array('*'),'left');
		$select->join('clf_users', 'clf_ads_order.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_user_details', 'clf_ads_order.user_id=clf_user_details.user_id',array('*'),'left');
		$select->where('clf_ads_order.status=1');
		$select->where('clf_ads_order.user_id="'.$userId.'"');
		if($ordertype!='' && $cid=='' && $scid==''){
			$select->where('clf_ads_order.ads_mode="'.$ordertype.'"');		
		}else if($ordertype!='' && $cid!='' && $scid==''){
			$select->where('clf_ads_order.ads_mode="'.$ordertype.'"');
			$select->where('clf_ads_main.category_id="'.$cid.'"');
		}else if($ordertype!='' && $cid!='' && $scid!=''){
			$select->where('clf_ads_order.ads_mode="'.$ordertype.'"');
			$select->where('clf_ads_main.category_id="'.$cid.'"');
			$select->where('clf_ads_main.sub_cat_id="'.$scid.'"');
		}else if($ordertype=='' && $cid!='' && $scid!=''){
			$select->where('clf_ads_main.category_id="'.$cid.'"');
			$select->where('clf_ads_main.sub_cat_id="'.$scid.'"');
		}else if($ordertype=='' && $cid!='' && $scid==''){
			$select->where('clf_ads_main.category_id="'.$cid.'"');
		}else if($ordertype=='' && $cid=='' && $scid!=''){
			$select->where('clf_ads_main.sub_cat_id="'.$scid.'"');
		}else if($ordertype=='' && $cid=='' && $scid=''){
			$select->where('clf_ads_order.user_id="'.$userId.'"');
		}
		$select->order('clf_ads_order.ads_order_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function franchiseeviewOrderList($userId,$district_id,$area_id,$location_id,$cid,$scid,$paymentType){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_ads_main', 'clf_ads_order.ads_order_id=clf_ads_main.ads_order_id',array('*'),'left');
		$select->join('clf_ads_info', 'clf_ads_order.ads_order_id=clf_ads_info.ads_posted_id',array('*'),'left');
		$select->join('clf_users', 'clf_ads_order.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_user_details', 'clf_ads_order.user_id=clf_user_details.user_id',array('*'),'left');
		if($cid!='' && $scid==''){
			$select->where('clf_ads_main.category_id="'.$cid.'"');
		}else if($cid!='' && $scid!=''){
			$select->where('clf_ads_main.category_id="'.$cid.'"');
			$select->where('clf_ads_main.sub_cat_id="'.$scid.'"');
		}else if($cid=='' && $scid!=''){
			$select->where('clf_ads_main.sub_cat_id="'.$scid.'"');
		}
		$select->where('clf_ads_main.district_id="'.$district_id.'"');
		$select->where('clf_ads_main.area_id="'.$area_id.'"');
		$select->where('clf_ads_order.ads_mode="'.$paymentType.'"');
		//$select->where('clf_ads_main.location_id="'.$location_id.'"');
		//$select->where('clf_ads_order.user_id="'.$userId.'"');
		$select->order('clf_ads_order.ads_order_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function orderSuccess($oId,$txId,$status){
		$data = array(
			'tx_id' 		       => $txId,
			'payment_status'	   => $status, 	
		);
		$this->tableGateway->update($data, array('ads_order_id' => $oId));	
		return $this->tableGateway->lastInsertValue;		
	}
	public function updateOrderstatus($orderId,$oStatus){
		$data = array(
			'order_status' 		=>	$oStatus,
		);
		$this->tableGateway->update($data, array('ads_order_id' => $orderId));	
		return $this->tableGateway->lastInsertValue;	
	}
	public function viewOrdersDetails($orderid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_ads_main', 'clf_ads_order.ads_order_id=clf_ads_main.ads_order_id',array('*'),'left');
		$select->where('clf_ads_order.ads_order_id="'.$orderid.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet->current();
	}
	public function updateFrenchseeStaus($orderid,$paidAmount,$paymentStatus){
		$data = array(
				'ads_order_id'        =>$orderid,
				'amount_paid'         =>$paidAmount,
				'order_status'        =>$paymentStatus,
				'payment_status'      =>$paymentStatus,
				);
		$updateStatus=$this->tableGateway->update($data, array('ads_order_id' => $data['ads_order_id']));
		return $this->tableGateway->lastInsertValue;			
	}
	public function checkTxtid($tntid){
		$select = $this->tableGateway->getSql()->select();
		$select->where('tx_id="'.$tntid.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet->count();
	}
	public function getOrderInfo_d($orderId){
		$select = $this->tableGateway->getSql()->select();
		$select->join('clf_users',  'clf_ads_order.user_id=clf_users.user_id',array('*'),'left');
		$select->join('clf_ads_main', 'clf_ads_order.ads_order_id=clf_ads_main.ads_order_id',array('*'),'left');
		$select->where('clf_ads_order.ads_order_id="'.$orderId.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet->current();
	}
}